package top.frsf.pattern.c_behavioral.c_observer.normal.pub;

import top.frsf.pattern.c_behavioral.c_observer.normal.sub.ISubscribe;

import java.util.ArrayList;
import java.util.List;

/**
 * 用户注册业务
 *
 * @author FRSF
 * @since 2024/9/4 22:38
 */
public class UserRegisterService implements IPublish{

    // 订阅者列表
    List<ISubscribe> subscribes = new ArrayList<>();

    @Override
    public void addSubscribe(ISubscribe subscribe) {
        subscribes.add(subscribe);
    }

    @Override
    public void removeSubscribe(ISubscribe subscribe) {
        subscribes.remove(subscribe);
    }

    @Override
    public void notifyAllSubscribes(String username) {
        subscribes.forEach(subscribes -> subscribes.update(username));
    }

    /**
     * 用户注册
     *
     * @param username 用户名
     * @param password 密码
     */
    public void register(String username, String password) {
        // 注册业务逻辑
        // ...
        // 通知所有订阅者
        notifyAllSubscribes(username);
    }
}
